﻿using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace demo09_桌面绘图
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }


        [DllImport("User32.dll")]
        public extern static IntPtr GetDC(System.IntPtr hWnd);
        private void button1_Click(object sender, EventArgs e)
        {
            IntPtr DesktopHandle = GetDC(IntPtr.Zero);
            Graphics g = System.Drawing.Graphics.FromHdc(DesktopHandle);  //创建一个画家对象，这个画家对象从桌面来，那它只能在桌面上画画。
            g.FillRectangle(new SolidBrush(Color.Red), 0, 0, 100, 100);  //填充一个矩形
            g.DrawEllipse(Pens.Blue, new Rectangle(100, 100, 300, 100));    //画椭圆
            g.DrawString("我是画出来的文字", new Font("微软雅黑", 12.1F), new SolidBrush(Color.Red), new PointF(200, 200)); //画出文字
            Image img = new Bitmap(@"雪人.png");
            g.DrawImage(img, new Point(300, 300));
            g.Dispose();

            //更好的方法是创建窗体,在窗体上绘制, 然后背景透明即可!!!
        }
    }
}
